/*
this is for my shell,haha
*/
#define _GNU_SOURCE
#include    <stdio.h>
#include    <stdlib.h>
#include    <sys/types.h>
#include    <unistd.h>
#define  BUF_SIZE

char ** split_cmd(char * buf);
int main()
{
    char *buf=NULL,*buf2=NULL;
    size_t buflen;
    ssize_t bufget;
    char ** argv;
    pid_t  pid;
    while(1)
    {
// get cmd string
        printf("start loop again>>>$ ");
        bufget = getline(&buf,&buflen,stdin);
        printf("buf addr is %X \n",buf);
        printf("get buflen is %ld,bufget is %ld,buf is %s\n",buflen,bufget,buf);

//splite cmd string
        argv = split_cmd(buf);
// handle cmd
        pid =fork();
        if(pid ==0)
        {
            execvp(argv[0],argv);
        }
        wait(NULL);
        //free this loop command buf;
        buf2=buf;
        free(buf2);
        buf=NULL;buf2=NULL;
    }

}
#define	is_delim(x) ((x)==' '||(x)=='\t')
char ** split_cmd(char * buf)
{
    char ** argv=NULL;
    int argc = 0 ;
    int pos=0,argv_pos=0;
    argv = malloc(1024);
    while (*(buf+pos)!='\n')
    {
        if(pos ==0)
        {
            *(argv+argc) =malloc(10);
        }
        if(!is_delim(*(buf+pos)))
        {
            (*(argv+argc))[argv_pos] = *(buf+pos);
            argv_pos++;
        }
        else
        {
            argc++;
            argv_pos =0;
            *(argv+argc) =malloc(10);
        }
        pos++;
    }
    return argv;
}